import os
import time

from selenium.webdriver.common.action_chains import ActionChains
from common.base import webdriver,sleep,By


class MouseOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/clicks.htm')

    def click(self):
        # TODO 单击第一种
        sleep(2)
        btn = self.driver.find_element(By.XPATH,'//form/input[3]')
        btn.click()

        # TODO 单击第二种
        sleep(2)
        ac = ActionChains(driver=self.driver)
        # 绑定方法之后，必须调用perform方法才会执行操作
        ac.click(btn).perform()

    def context_click(self):
        # 右击
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.XPATH,'/html/body/form/input[4]')
        ac.context_click(btn).perform()

    def double_click(self):
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.XPATH,'/html/body/form/input[2]')
        ac.double_click(btn).perform()

    def drag_and_drop(self):
        self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')
        time.sleep(2)
        ac = ActionChains(self.driver)
        source = self.driver.find_element(By.ID,'dragger')
        target = self.driver.find_element(By.XPATH,'//div[4]')
        ac.drag_and_drop(source,target).perform()

    def mouseover(self):
        self.driver.get('https://sahitest.com/demo/mouseover.htm')
        time.sleep(2)
        ac = ActionChains(self.driver)
        div = self.driver.find_element(By.XPATH,'//div/div')
        ac.move_to_element(div).perform()

    def click_and_hold(self):
        # 请求本地页面
        url = 'file:///' + os.path.abspath('html') + '/mouse_hold.html'
        self.driver.get(url)
        time.sleep(2)
        btn = self.driver.find_element(By.ID,'btn1')
        ac = ActionChains(self.driver)
        ac.click_and_hold(btn).perform()

    def quit(self):
        sleep(2)
        self.driver.quit()

if __name__ == '__main__':
    mouse = MouseOperation()
    # mouse.click()
    # mouse.quit()
    # mouse.context_click()
    # mouse.double_click()
    # mouse.drag_and_drop()
    # mouse.mouseover()
    mouse.click_and_hold()
    mouse.quit()

    # TODO 关于本地路径
    # print(os.path.abspath(--file - -))
    # print(os.path.abspath('html'))
    # print(os.path.dirname(os.path.abspath(--file - -)))